﻿using System;
using System.Collections.Generic;
using System.Text;

namespace DBScript
{
    /// <summary>
    /// 数据库查询(SQLite)
    /// </summary>
    public class SQLiteQuery:QueryBase
    {
        public SQLiteQuery()
        {
            ParamStyle = "$";
        }
        #region 输出SQL
        /// <summary>
        /// 输出SQL
        /// </summary>
        /// <returns></returns>
        public override string ExportSql()
        {
            if (!string.IsNullOrEmpty(Sql))
            {
                return ExportDeal(Sql);
            }
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat("select {0} from {1}", Columns, Table);
            if (!string.IsNullOrEmpty(Where))
            {
                sql.AppendFormat(" where {0}", Where);
            }
            return ExportDeal(sql.ToString());
        }

        public override string ExportModel()
        {
            if (!string.IsNullOrEmpty(Sql))
            {
                return ExportDeal(Sql);
            }
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat("select {0} from {1}", Columns, Table);
            if (!string.IsNullOrEmpty(Where))
            {
                sql.AppendFormat(" where {0}", Where);
            }
            sql.Append(" limit 0,1");
            return ExportDeal(sql.ToString());
        }

        public override string ExportList()
        {
            if (!string.IsNullOrEmpty(Sql))
            {
                return ExportDeal(Sql);
            }
            StringBuilder strSql = new StringBuilder();
            string strOrder; // -- 排序类型

            strOrder = string.Format("order by {0}", OrderBy);

            if (string.IsNullOrEmpty(Where))
            {
                strSql.Append(string.Format("select {0} from {1} {2} limit {3},{4}",
                                            Columns, Table, strOrder, LimitFirst-1, LimitLengh));
            }
            else
            {
                strSql.Append(string.Format("select {0} from {1} where {2} {3} limit {4},{5}",
                                            Columns, Table, Where, strOrder, LimitFirst-1, LimitLengh));
            }
            return ExportDeal(strSql.ToString());
        }

        public override string ExportUpdateFields()
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat("update {0} set {1}", Table, UpdateFields);
            if (!string.IsNullOrEmpty(Where))
            {
                sql.AppendFormat(" where {0}", Where);
            }
            return ExportDeal(sql.ToString());
        }

        public override string ExportDelete()
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat("delete from {0}", Table);
            if (!string.IsNullOrEmpty(Where))
            {
                sql.AppendFormat(" where {0}", Where);
            }
            return sql.ToString();
        }
        #endregion 输出SQL

        #region SQL输出处理
        public override string ExportDeal(string sql)
        {
            return sql.Replace("@", ParamStyle);
        }
        #endregion
    }
}
